package com.unswift.utils;

import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;
import com.unswift.annotation.api.ApiMethod;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

@Api(value="中文公共处理类", author="unswift", date="2023-05-13", version="1.0.0")
public final class ChineseUtils {
	
	@ApiMethod(value="汉字转拼音-带声调", params=@ApiField("汉字"), returns=@ApiField("拼音字符串"))
	public static String[] chineseToPinyinTone(char chinese){
		HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();
		format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
		format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        try {
        	return PinyinHelper.toHanyuPinyinStringArray(chinese, format);
		} catch (Exception e) {
			e.printStackTrace();
			throw ExceptionUtils.exception("chinese.to.pinyin.exception", e, e.getMessage());
		}
	}
	
	@ApiMethod(value="汉字转拼音-不带声调", params=@ApiField("汉字"), returns=@ApiField("拼音字符串"))
	public static String[] chineseToPinyin(char chinese){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
        	return PinyinHelper.toHanyuPinyinStringArray(chinese, format);
		} catch (Exception e) {
			e.printStackTrace();
			throw ExceptionUtils.exception("chinese.to.pinyin.exception", e, e.getMessage());
		}
	}
}
